import EditTool from "./lib/EditTool.js";
import * as geometryEngine from "@geoscene/core/geometry/geometryEngine";
import * as webMercatorUtils from "@geoscene/core/geometry/support/webMercatorUtils.js";
import Point from "@geoscene/core/geometry/Point.js";
import Polygon from "@geoscene/core/geometry/Polygon.js";
import Graphic from "@geoscene/core/Graphic.js";
import MathStuff from "./lib/MathStuff.js";
import { editSymbol } from "./lib/symbol";
import { measureArea, measureDistance } from "./lib/MeasureTool.js";

export default class MilitaryEditTool extends EditTool {
  callBack: any = [];
  distanceMeasure: any = null;
  areaMeasure: any = null;
  viewType: string = "2d";
  arrowType: string = "";
  arrowPoints: any = [];
  mathStuff: MathStuff = new MathStuff();

  constructor(options = { config: {} }) {
    super(options);
    this.viewType = this.appConfig.activeView.type;
    this.distanceMeasure = measureDistance(this.appConfig.activeView);
    this.areaMeasure = measureArea(this.appConfig.activeView);
    // console.log(this.distanceMeasure, this.areaMeasure);

    this.init();
  }

  /**
   *@Description: 初始化函数
   *@Parameters:
   *@MethodAuthor: yfyang
   *@Date: 2024-02-27 08:58:03
   */
  init() {
    //监听sketchViewModel 的创建事件
    this.sketchViewModel.on("create", (evt) => {
      this.modeUpdateFun(evt);
    });
  }

  modeUpdateFun(event: any) {
    // let that: MilitaryEditTool = this;

    let eventInfo = event.toolEventInfo;

    if (eventInfo && eventInfo.type === "vertex-add") {
      let addedVertex: Array<any> = eventInfo.added[0];
      let addPoint: Point = new Point({
        x: addedVertex[0],
        y: addedVertex[1],
        spatialReference: this.appConfig.activeView.spatialReference,
      });
      // 如果需要 WGS84 坐标，进行转换
      let addPointWGS84: Point;
      if (this.appConfig.activeView.spatialReference.isWebMercator) {
        addPointWGS84 = webMercatorUtils.webMercatorToGeographic(
          addPoint
        ) as Point;
      } else {
        // 如果地图已经是地理坐标系，直接使用
        addPointWGS84 = addPoint;
      }
      this.arrowPoints.push(addPointWGS84);
      if (
        (this.arrowType === "single" || this.arrowType === "double") &&
        this.arrowPoints.length === 2
      ) {
        let arrowLine = this.mathStuff.arrow2(
          this.arrowPoints[0],
          this.arrowPoints[1]
        );
        let arrowGraphic: Graphic = new Graphic({
          geometry: new Polygon({
            rings: arrowLine,
          }),
          symbol: editSymbol.pgcreateSymbol,
        });

        if (this.arrowType === "single") this.sketchViewModel.complete();
        this.tempLayer.removeAll();
        this.tempLayer.add(arrowGraphic);
      }
      if (this.arrowType === "double" && this.arrowPoints.length == 3) {
        let arrowLine = this.mathStuff.arrow2(
          this.arrowPoints[0],
          this.arrowPoints[1]
        );
        let arrowLine2 = this.mathStuff.arrow2(
          this.arrowPoints[0],
          this.arrowPoints[2]
        );
        let arrowGeometry: Polygon = new Polygon({
          rings: arrowLine,
        });
        let arrowGeometry2: Polygon = new Polygon({
          rings: arrowLine2,
        });
        let unionPolyon = geometryEngine.union([arrowGeometry, arrowGeometry2]);
        let arrowGraphic: Graphic = new Graphic({
          geometry: unionPolyon,
          symbol: editSymbol.pgcreateSymbol,
        });

        this.sketchViewModel.complete();
        this.tempLayer.removeAll();
        this.tempLayer.add(arrowGraphic);
      }
    }

    if (event.state === "active") {
      let addedVertex: Array<any> = eventInfo.coordinates;
      if (!addedVertex) return;
      let addPoint: Point = new Point({
        x: addedVertex[0],
        y: addedVertex[1],
        spatialReference: this.appConfig.activeView.spatialReference,
      });
      // 如果需要 WGS84 坐标，进行转换
      let addPointWGS84: Point;
      if (this.appConfig.activeView.spatialReference.isWebMercator) {
        addPointWGS84 = webMercatorUtils.webMercatorToGeographic(
          addPoint
        ) as Point;
      } else {
        // 如果地图已经是地理坐标系，直接使用
        addPointWGS84 = addPoint;
      }

      if (
        (this.arrowType === "single" || this.arrowType === "double") &&
        this.arrowPoints.length === 1
      ) {
        let arrowLine = this.mathStuff.arrow2(
          this.arrowPoints[0],
          addPointWGS84
        );
        let arrowGraphic: Graphic = new Graphic({
          geometry: new Polygon({
            rings: arrowLine,
          }),
          symbol: editSymbol.pgcreateSymbol,
        });
        this.tempLayer.removeAll();
        this.tempLayer.add(arrowGraphic);
      }
      if (this.arrowType === "double" && this.arrowPoints.length === 2) {
        let arrowLine1 = this.mathStuff.arrow2(
          this.arrowPoints[0],
          this.arrowPoints[1]
        );
        let arrowLine2 = this.mathStuff.arrow2(
          this.arrowPoints[0],
          addPointWGS84
        );
        let arrowPolyon1 = new Polygon({
          rings: arrowLine1,
        });
        let arrowPolyon2 = new Polygon({
          rings: arrowLine2,
        });
        let unionPolyon = geometryEngine.union([arrowPolyon1, arrowPolyon2]);
        let arrowGraphic: Graphic = new Graphic({
          geometry: unionPolyon,
          symbol: editSymbol.pgcreateSymbol,
        });
        this.tempLayer.removeAll();
        this.tempLayer.add(arrowGraphic);
      }
    }

    if (event.state === "complete") {
      this.arrowType = "";
      this.arrowPoints = [];
      if (this.callBack.length > 0) {
        this.callBack.forEach((fun: Function) => {
          fun(event.graphic);
        });
      }
    }
  }

  setCallBackMethod(fun: Function): void {
    this.callBack.push(fun);
  }

  createPoint(): void {
    this.createFeature("point", "click");
  }

  createPolyline(): void {
    this.createFeature("polyline", "click");
  }

  createPolygon(): void {
    this.createFeature("polygon", "click");
  }

  createCircle(): void {
    this.createFeature("circle", "click");
  }

  createRectangle(): void {
    this.createFeature("rectangle", "click");
  }

  createSingleArrow(): void {
    this.arrowType = "single";
    this.createFeature("polyline", "click");
  }

  createDoubleArrow(): void {
    this.arrowType = "double";
    this.createFeature("polyline", "click");
  }

  beginDistanceMeasure(): void {
    this.distanceMeasure.start();
  }

  beginAreaMeasure(): void {
    this.areaMeasure.start();
  }

  measureClear(): void {
    this.distanceMeasure.clear();
    this.areaMeasure.clear();
  }

  clear(): void {
    this.sketchViewModel.cancel();
    this.tempLayer.removeAll();
  }

  addGraphic(graphic: Graphic): void {
    this.tempLayer.add(graphic);
  }

  removeGraphic(graphic: Graphic): void {
    this.tempLayer.remove(graphic);
  }
}
